package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMerchantSettle;
import com.ruoyi.system.domain.HatchetMoneyChange;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IHatchetMoneyChangeService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.vo.HatchetMoneyChangeVo;

/**
 * 提现记录 信息操作处理
 * 
 * @author xj
 * @date 2019-02-18
 */
@Controller
@RequestMapping("/system/hatchetMoneyChange")
public class HatchetMoneyChangeController extends BaseController {
	private String prefix = "system/hatchetMoneyChange";

	@Autowired
	private IHatchetMoneyChangeService hatchetMoneyChangeService;
	@Autowired
	private IHatchetAgentService agentService;
	@Autowired
	private IHatchetMerchantService merchantService;
	@Autowired
	private ISysDictDataService sysDictDataService;
	@RequiresPermissions("system:hatchetMoneyChange:view")
	@GetMapping()
	public String hatchetMoneyChange() {
		return prefix + "/hatchetMoneyChange";
	}

	/**
	 * 查询提现记录列表
	 */
	@RequiresPermissions("system:hatchetMoneyChange:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetMoneyChange hatchetMoneyChange) {
		hatchetMoneyChange.setMoneyType("10B");

		BigDecimal sumMoney = hatchetMoneyChangeService.selectTixianSumMoney(hatchetMoneyChange);
		int index = 0;
		startPage();
		List<HatchetMoneyChange> list = hatchetMoneyChangeService.selectHatchetMoneyChangeList(hatchetMoneyChange);
		int stringLength;
		for (HatchetMoneyChange hatchetMoneyChange2 : list) {
			if (index == 0) {
				hatchetMoneyChange2.setSumMoney(sumMoney);
				index++;
			}
			if (hatchetMoneyChange2.getUserId() != null) {
				stringLength = hatchetMoneyChange2.getUserId().length();
				if (stringLength == 7) {
					hatchetMoneyChange2.setMerType("1");
					HatchetAgent agent = agentService.selectHatchetAgentByNo(hatchetMoneyChange2.getUserId());
					if (agent != null) {
						hatchetMoneyChange2.setMerName(agent.getAgentName());
					}
				}
				if (stringLength == 15) {
					hatchetMoneyChange2.setMerType("2");
					HatchetMerchant hatchetMerchant = merchantService
							.selectHatchetMerchantByNo(hatchetMoneyChange2.getUserId());
					if (hatchetMerchant != null) {
						hatchetMoneyChange2.setMerName(hatchetMerchant.getMerchantCnName());
					}
				}
			}
		}
		return getDataTable(list);
	}

	/**
	 * 导出提现记录列表
	 */
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(HatchetMoneyChange hatchetMoneyChange) {
		hatchetMoneyChange.setMoneyType("10B");
		List<HatchetMoneyChange> list = hatchetMoneyChangeService.selectHatchetMoneyChangeList(hatchetMoneyChange);
		List<HatchetMoneyChangeVo> listVo = new ArrayList<HatchetMoneyChangeVo>();
		for (HatchetMoneyChange moneyChange : list) {
			HatchetMoneyChangeVo moneyChangeVo = new HatchetMoneyChangeVo();
			moneyChangeVo.setID(moneyChange.getID());
			moneyChangeVo.setUserType(moneyChange.getUserType());
			moneyChangeVo.setTrxAmt(moneyChange.getTrxAmt());
			moneyChangeVo.setUserId(moneyChange.getUserId());
			moneyChangeVo.setSTATUS(moneyChange.getSTATUS());
			moneyChangeVo.setCreateTime(moneyChange.getCreateTime());
			listVo.add(moneyChangeVo);
		}
		ExcelUtil<HatchetMoneyChangeVo> util = new ExcelUtil<HatchetMoneyChangeVo>(HatchetMoneyChangeVo.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int stringLength;
		for (HatchetMoneyChangeVo hatchetMoneyChange2 : listVo) {
			hatchetMoneyChange2.setTime(sdf.format(hatchetMoneyChange2.getCreateTime()));
			hatchetMoneyChange2
					.setMoneyType(sysDictDataService.selectDictLabel("money_type", hatchetMoneyChange2.getMoneyType()));
			hatchetMoneyChange2.setExcelStatus(
					sysDictDataService.selectDictLabel("money_status", hatchetMoneyChange2.getSTATUS()));
			if (hatchetMoneyChange2.getUserId() != null) {
				stringLength = hatchetMoneyChange2.getUserId().length();
				if (stringLength == 7) {
					HatchetAgent agent = agentService.selectHatchetAgentByNo(hatchetMoneyChange2.getUserId());
					if (agent != null) {
						hatchetMoneyChange2.setMerName(agent.getAgentName());
					}
				}
				if (stringLength == 15) {
					HatchetMerchant hatchetMerchant = merchantService
							.selectHatchetMerchantByNo(hatchetMoneyChange2.getUserId());
					if (hatchetMerchant != null) {
						hatchetMoneyChange2.setMerName(hatchetMerchant.getMerchantCnName());
					}
				}
			}
		}
		return util.exportExcel(listVo, "提现记录表");
	}

	/**
	 * 提现状态修改——同意
	 */
	@Log(title = "提现管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:hatchetMoneyChange:edit")
	@PostMapping("/agreeStatus")
	@ResponseBody
	public AjaxResult agreeStatus(HatchetMoneyChange hatchetMoneyChange) {
		// 支付接口调用
		HatchetMoneyChange moneyChange = hatchetMoneyChangeService
				.selectHatchetMoneyChangeById(hatchetMoneyChange.getID());
		// 判断总分润金额-总提现金额=可提现金额；
		HatchetMoneyChange moneyChange1 = new HatchetMoneyChange();
		// 提现中
		moneyChange1.setUserId(moneyChange.getUserId());
		moneyChange1.setSTATUS("10B");
		BigDecimal tixian1 = hatchetMoneyChangeService.selectTixianSumMoney(moneyChange1);
		if (tixian1 == null) {
			tixian1 = new BigDecimal("0");
		}
		// 提现通过
		moneyChange1.setUserId(moneyChange.getUserId());
		moneyChange1.setSTATUS("10C");
		BigDecimal tixian2 = hatchetMoneyChangeService.selectTixianSumMoney(moneyChange1);
		if (tixian2 == null) {
			tixian2 = new BigDecimal("0");
		}
		BigDecimal fenrun = hatchetMoneyChangeService.selectSumFenRenChangeByUserId(moneyChange.getUserId());
		// 实际可提现金额
		BigDecimal aa = fenrun.subtract(tixian1).subtract(tixian2);

		HatchetMerchantSettle settle = new HatchetMerchantSettle();
		settle.setSettleNo(StringUtils.randomUUID());
		try {
			if ("10B".equals(moneyChange.getUserType())) {
				HatchetMerchant merchantByMerchantNo = merchantService
						.selectHatchetMerchantByNo(moneyChange.getUserId());
				// 实际余额与数据库余额对比
				int a = aa.compareTo(merchantByMerchantNo.getTotalMoney());
				if (a == -1) {
					return error("该商户提现金额异常，请核对分润总金额");
				}

			} else if ("10A".equals(moneyChange.getUserType())) {
				HatchetAgent hatchetAgent = new HatchetAgent();
				hatchetAgent.setAgentNo(moneyChange.getUserId());
				List<HatchetAgent> hatchetAgentList = agentService.selectHatchetAgentList(hatchetAgent);
				HatchetAgent agent = hatchetAgentList.get(0);
				// 实际余额与数据库余额对比
				int a = aa.compareTo(agent.getTotalMoney());
				if (a == -1) {
					return error("该代理提现金额异常，请核对分润总金额");
				}
			}
			// 修改提现订单状态
			moneyChange.setSTATUS("10C");
			return toAjax(hatchetMoneyChangeService.changeStatus(moneyChange));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 提现状态修改——拒绝
	 */
	@Log(title = "提现管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:hatchetMoneyChange:edit")
	@PostMapping("/refuseStatus")
	@ResponseBody
	public AjaxResult refuseStatus(HatchetMoneyChange hatchetMoneyChange) {
		// 退还提现金额
		HatchetMoneyChange moneyChange = hatchetMoneyChangeService
				.selectHatchetMoneyChangeById(hatchetMoneyChange.getID());
		String userType = moneyChange.getUserType();
		if ("10A".equals(userType)) {
			HatchetAgent agent = agentService.selectHatchetAgentByNo(moneyChange.getUserId());
			agent.setTotalMoney(agent.getTotalMoney().add(moneyChange.getTrxAmt()));
			agentService.updateHatchetAgent(agent);
		} else if ("10B".equals(userType)) {
			HatchetMerchant merchant = merchantService.selectHatchetMerchantByNo(moneyChange.getUserId());
			merchant.setTotalMoney(merchant.getTotalMoney().add(moneyChange.getTrxAmt()));
			merchantService.updateHatchetMerchant(merchant);
		}
		return toAjax(hatchetMoneyChangeService.changeStatus(hatchetMoneyChange));
	}

}
